package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Coleccion;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOColeccion;

public class MySQLDAOColeccion implements DAOColeccion {
	
	private Coleccion coleccion = null;
	
	
	public MySQLDAOColeccion() {
	}

	public int insert(Coleccion coleccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Coleccion ("
					+ "Id_Coleccion, Nombre "
					+ ") " + "values (?, ?) ");

			stmt.setLong(1, coleccion.getNroColeccion());
			stmt.setString(2, coleccion.getNombre());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Coleccion coleccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update Coleccion " 
							+ "set Nombre = ?"
							+ " where id_Coleccion = ? ");

			stmt.setString(1, coleccion.getNombre());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Coleccion coleccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from Coleccion where id_Coleccion = ? ");
			stmt.setLong(1, coleccion.getNroColeccion());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Coleccion> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Coleccion> coleccions = new ArrayList<Coleccion>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from Coleccion");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Coleccion coleccion = new Coleccion();
				coleccion.setNroColeccion(rs.getInt("Id_Coleccion"));
				coleccion.setNombre(rs.getString("Nombre"));
				coleccions.add(coleccion);
			}

			return coleccions;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Coleccion findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		coleccion = new Coleccion();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Coleccion where Id_Coleccion = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
            coleccion.setNroColeccion(rs.getInt("Id_Coleccion"));
            coleccion.setNombre(rs.getString("Nombre"));
			}

			return coleccion;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Coleccion coleccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Coleccion where UCASE(Nombre)");

			stmt.setString(1, coleccion.getNombre().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
